<?php
$starttime  = microtime(true);

//load config
include "../includes/config/config.php";
//create object db
include "../includes/class_mysql.php";
include "../includes/gpc_function.php";
include('includes/functions.php');
include('includes/global.php');
include('includes/functions_sitemap.php');
include_once ("../includes/phpmailer/function.sendmail.php");


getLastRun();


//script run :--------------------------------
//checkPromotionExdate();
//SendNewsLetterCron();


ImportClass('weather');
UpdateWeather();




//-------end script run-----------------------------
sitemap();
LogCronjob();



echo '<br>-=------------------------------------------------------=-';
echo '<br>total time: '.(microtime(true) - $starttime).'<br>';
//functions-----------------------------------------

function getLastRun()
{
    global $dbsql,$language;
    $get_Only   = gpc_getBoolGet('time');
    $now_time   = date('d/m/Y H:i:s');
    $last_run   = getConfig('LastRunCronjob');
    echo 'Last run: '.$last_run.'<br>';
    if($get_Only)
    {
        exit;
    }
    saveConfig('LastRunCronjob',$now_time);    
}
function checkPromotionExdate()
{
    global $dbsql,$language;
    $starttime  = microtime(true);
    $datenow    = date('Y-m-d');
    $sql_select = "select iId,iParentId,cType from tblpromotions where dEndDate < '$datenow' and dEndDate!='0000-00-00' ";
    $rs         = $dbsql->query($sql_select);
    while($result=$dbsql->fetch_array($rs))
    {
        $sql_delete = "delete from tblpromotions where iId=".$result['iId'];
        $dbsql->query($sql_delete);
        
        $table  = ($result['cType']=='tour')? "tblsettour" : "tblhotel";
        
        $sql_update = "update tblhotel set iPromotion = 0 where iId=".$result['iParentId'];
        $dbsql->query($sql_update);
        
    }
    
    echo '<b>checkpromotion</b>';
    echo '<br>->time: '.(microtime(true) - $starttime).'<br>==========================================<br>';
}

function SendNewsLetterCron()
{
    global $dbsql,$language,$root_domain;
    ImportClass('json');
    $starttime  = microtime(true);
    
    echo '<b>send newsletter</b><br>';
    $month      = date("m");
    $year       = date("Y");
    //select content newsletter:
    $sql_select = "select cName,cUrlString,iId from tblnewsletter where iStatus=1 order by iId desc limit 1";
    $rs         = $dbsql->query($sql_select);
    
    if( $result = $dbsql->fetch_array($rs) )
    {        
        echo 'send newsletter to : <br>';
        $sql_select="select iId from tblnewsletter_email where iStatus='1' and iMonth< ".$result['iId']." limit 30";
	    $rs        =$dbsql->query($sql_select);
        $listidEmail = array();
        while($rsemail  = $dbsql->fetch_array($rs))
        {
            $listidEmail[]=$rsemail['iId'];
        } 
        
        IncludeFunction('newsletter');
        
        SendNewsletter($result['iId'],$listidEmail);
        
    }else
    {
        echo 'no newsletter in month'.$month;
    }
    echo '<br>->time: '.(microtime(true) - $starttime).'<br>==========================================<br>';
}

function LogCronjob($msg='')
{
    global $ip;
    $msg    = date('Y-m-d H:i:s').'=> IP:'.$ip."\n\n".$msg;
    $file   = '../docs/log_cronjob.txt';
    $f      = @fopen($file,'a');
    @fwrite($f,$msg."\n\n");
    @fclose($f);    
}

function curl($url,$posts="",$cookie=0,$autoref=false) 
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	if (!empty($posts)) {
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
	}
	$header[0]= "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
	$header[1]= "Accept-Language: en-us,en;q=0.5";
	$header[2]= "Accept-Encoding: deflate";
	$header[3]= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
	$header[4]= "Connection: Keep-Alive";
	$header[5]="Cache-Control: no-cache";
    
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
	@curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
	curl_setopt($ch, CURLOPT_HEADER,1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	@curl_setopt($ch, CURLOPT_FOLLOWLOCATION,$autoref);
	if($cookie==1)
	{
		@curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");		
	}
	@curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
	ob_start();
	$result = curl_exec($ch);
	curl_close($ch);
	ob_end_clean();
	$return=explode("\r\n\r\n",$result,2);
	return $return[1];
}
?>